﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Stauffware.Common.Core
{
	/// <summary>
	/// Extension methods for use with System.Exception.
	/// </summary>
	public static class Exception_Extensions
	{
		/// <summary>
		/// Calls Exception.Data.Add(key, data) and returns the exception.
		/// This shorthand method allows adding data to an exception and
		/// throwing it all in one statement.  Example: 
		/// throw new Exception("uh-oh").AddData("when", DateTime.Now);
		/// </summary>
		public static Exception AddData (this Exception ex, string key, object data)
		{
			ex.Data.Add(key, data);
			return ex;
		}

		/// <summary>
		///	Returns a string representation of the exception, including information
		///	that ToString() doesn't provide: properties defined in sub-classes of
		///	Exception, and the contents of Exception.Data.
		/// </summary>
		public static string ToStringAll (this Exception ex)
		{
			return ErrorUtils.DumpExceptionChain(ex);
		}

		/// <summary>
		/// Returns a string containing the exception's message string and that of
		/// its inner exception (if any), recursively.
		/// </summary>
		/// <param name="ex">the exception</param>
		/// <param name="separator">a string that will be used separate the messages</param>
		/// <returns></returns>
		public static string GetAllMessages (this Exception ex, string separator)
		{
			return ErrorUtils.GetMessageChain(ex, separator);
		}
	}
}
